//
//  Accepted.swift
//  jiweiapp
//
//  Created by Kin Wong on 15/8/24.
//  Copyright (c) 2015年 Kin Wong. All rights reserved.
//

import UIKit

class AcceptedView: UIViewController, UITableViewDataSource, UITableViewDelegate {
    var tableview: UITableView?
    var titles = [String]()
    var details = [String]()
    var refresh = UIRefreshControl()
    var xmlData = XMLAnalyser()
    
    override func loadView() {
        super.loadView()
    }
        
    override func viewDidLoad() {
        super.viewDidLoad()
            
        self.view.backgroundColor = UIColor.whiteColor()
        self.tableview = UITableView(frame: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-60), style: UITableViewStyle.Plain)
        self.tableview!.tableFooterView = UIView(frame: CGRectZero)
        self.view.addSubview(self.tableview!)
        (titles, details) = xmlData.readLocalXML("Saved.xml")
        if self.titles.isEmpty {
            emptyMessage()
        }
        else {
            addItems()
        }
        
        refresh.addTarget(self, action: Selector("refreshData"), forControlEvents: UIControlEvents.ValueChanged)
        refresh.attributedTitle = NSAttributedString(string: "下拉刷新")
        refresh.tintColor = UIColor.blueColor()
        self.tableview!.addSubview(refresh)
        // Do any additional setup after loading the view.
    }
        
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func emptyMessage() {
        let alert = UIAlertView()
        alert.title = "空空的"
        alert.message = "这里是空空的，赶紧去接个任务吧！"
        alert.addButtonWithTitle("我知道咯")
        alert.show()
    }
    
    func addItems() {
        self.tableview!.delegate = self
        self.tableview!.dataSource = self
        self.tableview!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "AcceptedCell")
        self.view.addSubview(self.tableview!)
    }
    
    func refreshData() {
        (titles, details) = xmlData.readLocalXML("Saved.xml")
        self.tableview?.reloadData()
        NSThread.sleepForTimeInterval(1.0)
        self.refresh.endRefreshing()
        if self.titles.isEmpty {
            emptyMessage()
        }
        else {
            addItems()
        }
    }
    
    func numberOfSection(tableView: UITableView!) -> Int {
        return 1
    }
        
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.titles.isEmpty == true {
            return 0
        }else {
            return self.titles.count
        }
    }
        
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identify: String = "AcceptedCell"
        
        var cell = tableview?.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as! UITableViewCell
        cell.accessoryType = UITableViewCellAccessoryType.None
        cell.textLabel?.text = self.titles[indexPath.row]
        cell.detailTextLabel?.text = self.details[indexPath.row]
        
        return cell
    }
        
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.tableview!.deselectRowAtIndexPath(indexPath, animated: true)
        
        var itemString = self.titles[indexPath.row]
        var itemDetail = self.details[indexPath.row]
        
        var detail = DetailsView2()
        detail.itemString = itemString
        detail.itemDetail = itemDetail
        detail.title = itemString
        self.navigationController?.pushViewController(detail, animated: true)
    }
    
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            self.titles.removeAtIndex(indexPath.row)
            self.details.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
            xmlData.writeLocalXML("Saved.xml", titles: self.titles, details: self.details)
        }
    }
    
    func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String! {
        return "完成"
    }
}
